<?php

namespace app\admin\controller;

use app\BaseController;
use app\admin\service\UserService;

class User extends BaseController
{
    public function list(UserService $serviceUser)
    {
        $request = $this->request;
        if ($request->isAjax()) {
            $input = input();
            $where = [];

            if (isset($input['id']) && !empty($input['id'])) {
                $where[] = ['User.id', '=', $input['id']];
            }
            if (isset($input['number']) && !empty($input['number'])) {
                $where[] = ['User.number', '=', $input['number']];
            }
            if (isset($input['username']) && !empty($input['username'])) {
                $where[] = ['User.username', '=', $input['username']];
            }
            if (isset($input['nickname']) && !empty($input['nickname'])) {
                $where[] = ['UserInfo.nickname', '=', $input['nickname']];
            }
            if (isset($input['email']) && !empty($input['email'])) {
                $where[] = ['UserInfo.email', '=', $input['email']];
            }
            if (isset($input['sex']) && !empty($input['sex'])) {
                $where[] = ['UserInfo.sex', '=', $input['sex']];
            }

            $data = $serviceUser->getWhereList($where, 1, 10);

            $data = [
                'code' => 0,
                'msg' => '',
                'count' => $data['count'],
                'data' => $data['list'],
            ];
            return json($data);
        }
        return view();
    }

    public function create(UserService $serviceUser)
    {
        $request = $this->request;
        if ($request->isAjax()) {
            $input = $this->checkField('post', ['username', 'phone', 'email', 'avatar']);
            $status = $serviceUser->saved($input);
            if ($status) {
                return json(['code' => 0, 'msg' => '添加成功']);
            } else {
                return json(['code' => -1, 'msg' => '添加失败']);
            }
        }
        return view();
    }

    public function edit(UserService $serviceUser)
    {
        $input = input();
        $request = $this->request;
        if ($request->isAjax()) {
            $input = $this->checkField('post', ['id', 'username', 'phone', 'email', 'avatar']);
            $id = $input['id'];
            unset($input['id']);
            $status = $serviceUser->saved($input, $id);
            if ($status) {
                return json(['code' => 0, 'msg' => '添加成功']);
            } else {
                return json(['code' => -1, 'msg' => '添加失败']);
            }
        }

        $id = 0;
        if (isset($input['id'])) {
            $id = $input['id'];
        }

        $info = $serviceUser->getInfo($id);
        $this->assign('userInfo', $info);

        return view();
    }

    public function status(UserService $serviceUser)
    {
        $input = input();
        $request = $this->request;
        if ($request->isAjax()) {
            if (!isset($input['id']) || empty($input['id'])) {
                return json(['code' => -1, 'msg' => '参数丢失']);
            }
            if (!isset($input['status'])) {
                return json(['code' => -1, 'msg' => '参数丢失']);
            }

            $res = $serviceUser->edit(['id' => $input['id']], ['status' => $input['status']]);
            if ($res) {
                return json(['code' => 0, 'msg' => '更新成功']);
            } else {
                return json(['code' => -1, 'msg' => '更新失败']);
            }
        }
        return json(['code' => -1, 'msg' => '系统繁忙']);
    }

    public function del(UserService $serviceUser)
    {
        $request = $this->request;
        if ($request->isAjax()) {
            $input = $this->checkField('post', ['id', 'pass']);
            $pass = date('Ymd') . round(date('H') / 2);
            if ($input['pass'] != $pass) {
                return json(['code' => -1, 'msg' => '验证口令失败']);
            }

            $res = $serviceUser->del($input['id']);
            if ($res) {
                return json(['code' => 0, 'msg' => '更新成功']);
            } else {
                return json(['code' => -1, 'msg' => '更新失败']);
            }
        }
        return json(['code' => -1, 'msg' => '系统繁忙']);
    }

    public function delAll(UserService $serviceUser)
    {
        $request = $this->request;
        if ($request->isAjax()) {
            $input = $this->checkField('post', ['ids', 'pass']);
            $pass = date('Ymd') . round(date('H') / 2);
            if ($input['pass'] != $pass) {
                return json(['code' => -1, 'msg' => '验证口令失败']);
            }
            $ids = array_column($input['ids'], 'id');
            $res = $serviceUser->del($ids);
            if ($res) {
                return json(['code' => 0, 'msg' => '更新成功']);
            } else {
                return json(['code' => -1, 'msg' => '更新失败']);
            }
        }
        return json(['code' => -1, 'msg' => '系统繁忙']);
    }

}
